//
//  StockDetialVC.m
//  timelyRain
//
//  Created by Jiafei Wei on 16/8/15.
//  Copyright © 2016年 Jiafei Wei. All rights reserved.
//

#import "StockDetialVC.h"
#import "MBTUiColor.h"
#import "HttpTool.h"
#import "stockDetail.h"
#import "StockData.h"
#import "StockResult.h"
#import "DbManage.h"

#define TextLableColor @"#B0E5B8"
#define TitleBackGroundColor @"#1186D6"
#define isWarningTrueImage @"开按钮"
#define isWarningFalseImage @"关按钮"

@interface StockDetialVC ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIView *staus;
@property (weak, nonatomic) IBOutlet UIView *nav;
@property (weak, nonatomic) IBOutlet UIView *left;
@property (weak, nonatomic) IBOutlet UIView *right;
@property (weak, nonatomic) IBOutlet UILabel *titleText;
@property (weak, nonatomic) IBOutlet UIView *stockInfo;
@property (weak, nonatomic) IBOutlet UILabel *nowPrice;
@property (weak, nonatomic) IBOutlet UILabel *todayOpen;
@property (weak, nonatomic) IBOutlet UILabel *heightest;
@property (weak, nonatomic) IBOutlet UILabel *nowPriceNumber;
@property (weak, nonatomic) IBOutlet UILabel *todayOpenNumber;
@property (weak, nonatomic) IBOutlet UILabel *heightNumber;
@property (weak, nonatomic) IBOutlet UILabel *increaseOrFall;
@property (weak, nonatomic) IBOutlet UILabel *yesterday;
@property (weak, nonatomic) IBOutlet UILabel *lowest;
@property (weak, nonatomic) IBOutlet UILabel *increaseOrFallNumber;
@property (weak, nonatomic) IBOutlet UILabel *yesterdayNumber;
@property (weak, nonatomic) IBOutlet UILabel *lowestNumber;
@property (weak, nonatomic) IBOutlet UILabel *amplitude;//振幅
@property (weak, nonatomic) IBOutlet UILabel *amplitudeNumber;
@property (weak, nonatomic) IBOutlet UILabel *Increase;
@property (weak, nonatomic) IBOutlet UILabel *IncreaseNumber;
@property (weak, nonatomic) IBOutlet UILabel *change;
@property (weak, nonatomic) IBOutlet UILabel *changeNumber;
@property (weak, nonatomic) IBOutlet UITextField *upTo;
@property (weak, nonatomic) IBOutlet UIButton *isUpTo;
@property (weak, nonatomic) IBOutlet UITextField *fallTo;
@property (weak, nonatomic) IBOutlet UIButton *isFallTo;
@property (weak, nonatomic) IBOutlet UITextField *upWidth;
@property (weak, nonatomic) IBOutlet UIButton *isUpWith;
@property (weak, nonatomic) IBOutlet UITextField *fallWidth;
@property (weak, nonatomic) IBOutlet UIButton *isFallWidth;
@property (weak, nonatomic) IBOutlet UIButton *complete;
- (IBAction)ok:(id)sender;
- (IBAction)isFallToSetting:(id)sender;

- (IBAction)isUpToSetting:(id)sender;
- (IBAction)isUpWithSetting:(id)sender;
- (IBAction)isFallWidthSetting:(id)sender;

@end

@implementation StockDetialVC

- (void)viewDidLoad{
    [super viewDidLoad];
    [self updateStock:_codeInfo.stockcode];
    [self setWaring];
    [self setTextColor];
    [self setViewBG];
    [self completeBack];
}
-(void) setWaring{
    _titleText.text=_codeInfo.name;
    _upTo.text=_codeInfo.upNumber;
    _fallTo.text=_codeInfo.downNumber;
    _upWidth.text=_codeInfo.increaseNumber;
    _fallWidth.text=_codeInfo.fallNumber;
    //是否打开
    if ([_codeInfo.isUpNumber isEqualToString:@"YES"]) {
        [_isUpTo setBackgroundImage:[UIImage imageNamed:isWarningTrueImage]forState:UIControlStateNormal];
    }else{
        [_isUpTo setBackgroundImage:[UIImage imageNamed:isWarningFalseImage ]forState:UIControlStateNormal];
    }
    if ([_codeInfo.isDownNumber isEqualToString:@"YES"]) {
        [_isFallTo setBackgroundImage:[UIImage imageNamed:isWarningTrueImage ]forState:UIControlStateNormal];
    }else{
        [_isFallTo setBackgroundImage:[UIImage imageNamed:isWarningFalseImage ]forState:UIControlStateNormal];
    }
    if ([_codeInfo.isFallNumber isEqualToString:@"YES"]) {
        [_isFallWidth setBackgroundImage:[UIImage imageNamed:isWarningTrueImage ]forState:UIControlStateNormal];
    }else{
        [_isFallWidth setBackgroundImage:[UIImage imageNamed:isWarningFalseImage ]forState:UIControlStateNormal];
    }
    if([_codeInfo.isIncreaseNumber isEqualToString:@"YES"]) {
        [_isUpWith setBackgroundImage:[UIImage imageNamed:isWarningTrueImage ]forState:UIControlStateNormal];
    }else{
        [_isUpWith setBackgroundImage:[UIImage imageNamed:isWarningFalseImage]forState:UIControlStateNormal];
    }

}
-(void)setTextColor{
    _nowPrice.textColor =[UIColor colorwithHexString:TextLableColor];
     _todayOpen.textColor =[UIColor colorwithHexString:TextLableColor];
     _heightest.textColor =[UIColor colorwithHexString:TextLableColor];
    _increaseOrFall.textColor =[UIColor colorwithHexString:TextLableColor];
   _yesterday.textColor =[UIColor colorwithHexString:TextLableColor];
    _lowest.textColor =[UIColor colorwithHexString:TextLableColor];
    _amplitude.textColor =[UIColor colorwithHexString:TextLableColor];
    _Increase.textColor =[UIColor colorwithHexString:TextLableColor];
    _change.textColor =[UIColor colorwithHexString:TextLableColor];
}


-(void)setViewBG{
    self.view.backgroundColor=[UIColor colorwithHexString:@"#F4F3F8"];
    self.navigationController.navigationBarHidden=YES;
    _staus.backgroundColor =[UIColor colorwithHexString:TitleBackGroundColor];
    _nav.backgroundColor =[UIColor colorwithHexString:TitleBackGroundColor];
    _left.backgroundColor =[UIColor colorwithHexString:TitleBackGroundColor];
    _right.backgroundColor =[UIColor colorwithHexString:TitleBackGroundColor];
    _stockInfo.backgroundColor =[UIColor colorwithHexString:TitleBackGroundColor];
}

-(void)completeBack{
    _left.userInteractionEnabled = YES;
    UITapGestureRecognizer *addTapGest = [[UITapGestureRecognizer alloc] init];
    addTapGest.numberOfTapsRequired = 1;
    addTapGest.delegate = self;
    [_left addGestureRecognizer:addTapGest];
    [addTapGest addTarget:self action:@selector(addTapView:)];
}
- (void)addTapView:(UITapGestureRecognizer *)addTapGest{
    [self.navigationController popViewControllerAnimated:YES];
}

//做一个for循环

//在这里提示框
-(void)updateStock:(NSString *)stockId{//数据解析成功
    NSDictionary *params = @{@"gid":stockId,@"key":@"0364782500bf476ffc1dc7557d18d5e5"};
    [HttpTool GET:@"http://web.juhe.cn:8080/finance/stock/hs" WithHeader:NO parameters:params success:^(id responseObject) {
        NSLog(@"00000000");
        stockDetail *text =[stockDetail mj_objectWithKeyValues:responseObject];
        NSArray *result=text.result;
        StockResult *ddd =[result objectAtIndex:0];
        StockData *data =ddd.data;
        _nowPriceNumber.text=data.nowPri;
        _todayOpenNumber.text=data.todayStartPri;
       _heightNumber.text=data.todayMax;
        _increaseOrFallNumber.text=data.increase;
       _yesterdayNumber.text =data.yestodEndPri;
      _lowestNumber.text =data.todayMin;
        _amplitudeNumber.text=data.increPer;
        } failure:^(NSError *error){
            NSLog(@"错误是什么啊＝＝＝＝%@",error);
    }];
}
- (IBAction)ok:(id)sender{
    _codeInfo.upNumber=_upTo.text;
    _codeInfo.downNumber=_fallTo.text;
    _codeInfo.increaseNumber=_upWidth.text;
   _codeInfo.fallNumber= _fallWidth.text;
    [[DbManage shareDbManage]updateStock:_codeInfo];
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)isFallToSetting:(id)sender {
    if ([_codeInfo.isDownNumber isEqualToString:@"YES"]) {
        [_isFallTo setBackgroundImage:[UIImage imageNamed:isWarningFalseImage ]forState:UIControlStateNormal];
        _codeInfo.isDownNumber =@"NO";
    }else{
        [_isFallTo setBackgroundImage:[UIImage imageNamed:isWarningTrueImage ]forState:UIControlStateNormal];
        _codeInfo.isDownNumber =@"YES";
    }
}

- (IBAction)isUpToSetting:(id)sender {
     NSLog(@"isUpToSetting");
    if ([_codeInfo.isUpNumber isEqualToString:@"YES"]) {
        [_isUpTo setBackgroundImage:[UIImage imageNamed:isWarningFalseImage]forState:UIControlStateNormal];
        _codeInfo.isUpNumber =@"NO";
    }else{
        [_isUpTo setBackgroundImage:[UIImage imageNamed:isWarningTrueImage ]forState:UIControlStateNormal];
         _codeInfo.isUpNumber =@"YES";
    }
}

- (IBAction)isUpWithSetting:(id)sender {
    if([_codeInfo.isIncreaseNumber isEqualToString:@"YES"]) {
        [_isUpWith setBackgroundImage:[UIImage imageNamed:isWarningFalseImage ]forState:UIControlStateNormal];
         _codeInfo.isIncreaseNumber =@"NO";
    }else{ 
        [_isUpWith setBackgroundImage:[UIImage imageNamed:isWarningTrueImage]forState:UIControlStateNormal];
         _codeInfo.isIncreaseNumber =@"YES";
    }

}

- (IBAction)isFallWidthSetting:(id)sender {
    if ([_codeInfo.isFallNumber isEqualToString:@"YES"]) {
        [_isFallWidth setBackgroundImage:[UIImage imageNamed:@"关按钮" ]forState:UIControlStateNormal];
         _codeInfo.isFallNumber =@"NO";
    }else{
        [_isFallWidth setBackgroundImage:[UIImage imageNamed:@"开按钮" ]forState:UIControlStateNormal];
        _codeInfo.isFallNumber =@"YES";
    }
}

@end
